package com.kadoufall.recommender.dao;

import com.kadoufall.recommender.model.UserOperation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface UserOperationRepository extends JpaRepository<UserOperation, Long> {

    UserOperation findById(long id);

    List<UserOperation> findByUserId(long userId);

    List<UserOperation> findByUserIdOrderByReadTime(long userId);

    @Query("select uo from UserOperation uo where uo.userId = ?1 and uo.newsId = ?2")
    List<UserOperation> findByUserIdAndNewsId(Long userId, Long newsId);

    @Modifying
    @Transactional
    @Query("delete from UserOperation uo where uo.userId = ?1 and uo.newsId = ?2")
    void deleteByUserIdAndNewsId(Long userId, Long newsId);
}
